
$(function(){
	
	var isEdit = Number(getParmasByUrl(location.href, 'isEdit'));
	var id = null;
	// var address = null;
	if (isEdit) {
		//编辑操作
		//将数据展现在页面中
		if (localStorage.getItem('editAddress')) {
			 var address = JSON.parse(localStorage.getItem('editAddress'));
			id = address.id;
			var html = template('editTpl', address);
			$('.mui-input-group').html(html);
			var url = '/address/updateAddress';
		} else {
			var url = '/address/addAddress';
		}
	} else {

		var html = template('editTpl', {});
		$('.mui-input-group').html(html);
	}
	//创建poppicker组件；
	var picker = new mui.PopPicker({layer:3}); 
	//给picker添加数据
	picker.setData(cityData); 
	//给 省市名 添加轻敲事件
	$('#city').on('tap',()=>{
		picker.show((selectItems)=>{
			console.log(selectItems);
			$('#city').val(selectItems[0].text + selectItems[1].text + selectItems[2].text)
		}) ;
		
	})
	
	
	$('.mui-input-group').on('tap','.reg',()=>{
		// console.log(address);
		
		var username = $('input[name="name"]').val();
		var email = $('input[name="email"]').val();
		var city = $('input[name="city"]').val();
		var address = $('input[name="address"]').val();
		if(!username){
			mui.toast('请输入收货人姓名');
			return;
		}

		if (!email) {
			mui.toast('请输入邮编');
			return;
		}

		if (!city) {
			mui.toast('请输入地址');
			return;
		}

		if (!address) {
			mui.toast('请输入详细地址');
			return;
		}
		var data = {
			address: city,
			addressDetail: address,
			recipients: username,
			postcode: email,
		}
		// data.id = 
		if (isEdit) {
			//编辑操作
			//将数据展现在页面中
				var url = '/address/updateAddress';
				data.id = id;
				
		} else {
			var url = '/address/addAddress';
		}

		$.ajax({
			url:url,
			type:'post',
			data:data,
			success:(res)=>{
				if (res.success){
					if (isEdit){
						mui.toast('地址修改成功');
					}else{
						mui.toast('地址添加成功');
					}
					setTimeout(() => {
						location.href='adress.html'
					}, 1000);

				}
			
			}
		})

	})

	
})